#pragma once
#include "Helper.hpp"
#include "Tokenizer.hpp"

namespace Welp
{
  class Parser
  {
  public:
    Tokenizer mTokenizer;
    Token mToken;

    Parser();

    static void DefaultErrorCallback(const string& message, const Location& location, void* userData);

    void ReadNextToken();

    void Error(const string& message);
    bool Accept(TokenType::Enum type);
    bool Expect(TokenType::Enum type, const char* optionalError = nullptr);
    
    void ParseChunk();
    void ParseBody();
    bool ParseStatement();
    bool ParseFreeStatement();
    bool ParseDelimitedStatement();
    bool ParseIf();
    bool ParseWhile();
    bool ParseFunction();
    void ParseExpression();

    void Parse();
  };

  class ParserBacktracker
  {
  public:
    
    size_t mTokenPositionStart;
    bool mAccepted;
    Parser* mParser;

    ParserBacktracker(Parser* parser);

    // Rejects the tokens at their position
    ~ParserBacktracker();

    // Tells the parser to accept the token at this position (always returns true)
    bool Accept();
  };
}
